package com.example.yuejian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.yuejian.Adapter.PostAdapter;
import com.example.yuejian.Bean.Post;
import com.example.yuejian.R;
import com.example.yuejian.banners.BannerView;
import com.example.yuejian.activity.AddPost;
import com.example.yuejian.listener.ClubListener;
import com.example.yuejian.model.PostModel;

import java.util.ArrayList;
import java.util.List;




public class PostFragment extends Fragment implements ClubListener<Post> {
    View view;
    private List<View> viewList;
    BannerView bannerView;
    private int[] imgs = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four};
    private RecyclerView recyclerView_trainlist;
    private ImageView add;
    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.shaitu, null) ;
        recyclerView_trainlist =(RecyclerView)view.findViewById(R.id.recyclerView_trainlist);
        recyclerView_trainlist.setLayoutManager(new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL));
        add = (ImageView)view.findViewById(R.id.add) ;
        initBannerView();
        PostModel clubModel = new PostModel();
        clubModel.clubData(this);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddPost.class);
                startActivity(intent);
            }
        });
        return view ;
    }

    private void initBannerView(){

        viewList = new ArrayList<View>();

        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }
        bannerView = (BannerView) view.findViewById(R.id.banner);
        bannerView.startLoop(true);

        bannerView.setViewList(viewList);
        bannerView.setTransformAnim(true);
    }

    @Override
    public void onResponse(Post post) {
        List<Post.PostlistBean> clublistBeen = post.getPostlist();
        PostAdapter adapter = new PostAdapter(getActivity(),clublistBeen);
        recyclerView_trainlist.setAdapter(adapter);
    }

    @Override
    public void onFail(String msg) {

    }
}
